
#include <stdlib.h>
#include <math.h>

#include <util/msg.h>
#include "../../matrix/matrix.h"

static real scharr_data_3x3_Dx[9] = {+3, 0, -3, +10, 0, -10, +3, 0, -3};
static real scharr_data_3x3_Dy[9] = {+3, +10, +3, 0, 0, 0, -3, -10, -3};

static real scharr_data_3x1_Dx[3] = {+3, +10, +3};
static real scharr_data_1x3_Dx[3] = {1, 0, -1};

static real scharr_data_3x1_Dy[3] = {1, 0, -1};
static real scharr_data_1x3_Dy[3] = {+3, +10, +3};

/* ========================================================================== */

ImpMatrix imp_filter_scharr(int dim, char or, int comp) {
	ImpMatrix filt;

	if (dim != 3) {
		msg_error("Dimension %d not avaliable.\n", dim);
	}
	if (or != 'x' && or != 'y') {
		msg_error("Orientation '%c' not avaliable.\n", or);
	}
	if (comp < 0 || comp > 2) {
		msg_error("Component %d must be:\n"
				"\t0: for full matrix.\n"
				"\t1: for left nx1 vector.\n"
				"\t2: for right 1xn vector", dim);
	}

	// <editor-fold defaultstate="collapsed" desc="3x3 filter components">
	if (dim == 3 && comp == 0) {
		filt.rows = dim;
		filt.columns = dim;
		filt.size = dim*dim;
		if (or == 'x') filt.data = scharr_data_3x3_Dx;
		if (or == 'y') filt.data = scharr_data_3x3_Dy;
	}
	if (dim == 3 && comp == 1) {
		filt.rows = dim;
		filt.columns = 1;
		filt.size = dim;
		if (or == 'x') filt.data = scharr_data_3x1_Dx;
		if (or == 'y') filt.data = scharr_data_3x1_Dy;
	}
	if (dim == 3 && comp == 2) {
		filt.rows = 1;
		filt.columns = dim;
		filt.size = dim;
		if (or == 'x') filt.data = scharr_data_1x3_Dx;
		if (or == 'y') filt.data = scharr_data_1x3_Dy;
	}// </editor-fold>

	// =========================================================================

	return filt;
}
